<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Services\MoneyTransferService;
use App\Services\SearchTimeLimitService;
use common\enum\MoneyTransferEnum;
use Illuminate\Http\Request;
use App\Services\RoleService;
use Illuminate\Support\Facades\Log;

/**
 * 第三方转账
 */
class TransferController extends BaseController
{
    /**
     * 获取转账记录状态列表(除了异常状态)
     */
    public function getStatus()
    {
        return [
//            ['status' => MoneyTransferEnum::TRANSFER_STATUS_PENDING, 'name' => '待处理'],
            ['status' => MoneyTransferEnum::TRANSFER_STATUS_SUCCESS, 'name' => '成功'],
            ['status' => MoneyTransferEnum::TRANSFER_STATUS_FAILED, 'name' => '失败'],
        ];
    }

    /**
     * 获取额度转账钱包列表， 主钱包id=0
     */
    public function getWalletList(){
        $data = MoneyTransferService::getWalletList();
        return $data;
    }

    /**
     * 转账记录
     * @param Request $request
     * @return array
     */
    public function getTransferList(Request $request)
    {
        $params = $request->input();
        $params = amount_format_array($params, ['amount_from', 'amount_to'], true);
        //搜索时间限制
        if (!empty($params['time_begin']) && !empty($params['time_end'])) {
            SearchTimeLimitService::timeLimit($params['time_begin'],$params['time_end'],SearchTimeLimitService::SIX_MONTH);
        }
        $data = MoneyTransferService::getTransferList($params);
        $data = amount_format_array($data, ['amount']);
        return $data;
    }

    /**
     * 手动处理转账异常单 成功 或 失败
     * @param Request $request
     * @return array
     * @throws \App\Exceptions\CustomException
     * @throws \common\exception\CustomException
     */
    public function handle(Request $request)
    {
        $params = $this->validate($request, [
            'id' => 'required',
            'status' => 'required',
        ]);
        $data = MoneyTransferService::handle($params);
        return $data;
    }

    //补单检测结果 - 根据结果返回类型： 直接订单检测，  时间范围订单返回
    public function getTransferSupplement(Request $request)
    {
        $params = $this->validate($request, [
            'id' => 'required',
        ]);
        $data = MoneyTransferService::getTransferSupplement($params['id']);
        $data = amount_format_array($data, ['amount', 'balance']);
        return $data;
    }

    //获取转账异常告警订单数
    public function exceptionNum()
    {
        $data = MoneyTransferService::exceptionNum();
        return $data;
    }
}
